package alert

import (
	"main/handlers"
	"net/http"

	"github.com/gin-gonic/gin"
)

// GetAlertList 处理获取告警数据的请求
func GetAlertList(c *gin.Context) {

	query := `  
		SELECT a.*,b.alarm_config_name,b.alarm_config_level_type,d.project_name,d.project_id,e.code_name AS alert_type_name, f.alarm_config_group_url, COUNT(*) AS alarm_count, MIN(alarm_event_time) AS first_time, MAX(alarm_event_time) AS last_time
		FROM alarm_event a
		LEFT JOIN alarm_config b ON b.alarm_config_id = a.alarm_config_id
		LEFT JOIN sys_code e ON b.alarm_config_level_type = e.code_value
		LEFT JOIN alarm_config_group f ON f.alarm_config_group_id = b.alarm_config_group_id
		LEFT JOIN project d ON f.project_id = d.project_id
		WHERE e.code_type = 'alarm_level_type'
		AND a.alarm_event_alarm_status = 1
		AND a.alarm_event_exec_status = 1
		AND a.alarm_event_status != 1
		GROUP BY a.alarm_config_id
		ORDER BY a.alarm_event_id DESC
	`
	// 执行查询
	results, err := handlers.ExecuteQuery(query)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "Error executing query", "details": err.Error()})
		return
	}

	// 返回 JSON 响应
	c.JSON(http.StatusOK, results)
}
